<?php
// +----------------------------------------------------------------------
// | SwiftPHP [ JUST DO ONE THING WELL ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2017  http://swiftphp.zhongwenyu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhongwenyu <zhongwenyu1987@163.com> <http://www.zhongwenyu.com>
// +----------------------------------------------------------------------
// | Times: 2017/3/23 11:03
// +----------------------------------------------------------------------
namespace swift;

class Oss{
    protected $config = [
        // 配置
        'driver'               =>  'aliyun',
        'key'                  =>  'LTAI4wkTvzJ2Kx9n',
        'secret'               =>  'bvNiPFksXnm5OfWll6ESaiCQfS269R',
        'domain'               =>  'http://cdn.zhongwenyu.com',
        'bucket'               =>  'cdnspace',
    ];
    protected $driver = null;  // 驱动

    /**
     * 构造函数
     * @param array $config
     */
    public function __construct($config = []){
        !is_null(Config::get('oos')) && $this->config = Config::get('oos');

        if(!empty($config)){
            $this->config = array_merge($this->config , $config);
        }

        if(isset($this->config['driver']) && !empty($this->config['driver'])){
            $class = '\\swift\\oss\\driver\\' . strtolower($this->config['driver']);
            $this->driver = new $class($this->config);
        }
    }

    /**
     * 上传文件
     * @param string $file
     * @param string $dir
     * @return mixed
     */
    public function upload($file = '' , $dir = ''){
        return $this->driver->upload($file , $dir);
    }

    /**
     * 删除文件
     * @param $file
     * @return mixed
     */
    public function delete($file){
        return $this->driver->delete($file);
    }

    /**
     * 创建目录
     * @param string $dir
     * @return void
     */
    public function createdir($dir = ''){
        return $this->driver->delete($dir);
    }

    /**
     * 判断目录是否存在
     * @param string $dir
     * @return mixed
     */
    public function existdir($dir = ''){
        return $this->driver->existdir($dir);
    }

    /**
     * 下载文件
     * @param $filename
     * @return mixed
     */
    public function download($filename){
        return $this->driver->download($filename);
    }

    /**
     * 获取目录下的文件列表
     * @param string $dir
     * @param bool $recur  是否递归获取
     * @return mixed
     */
    public function getfiles($dir , $recur = false){
        return $this->driver->getfiles($dir , $recur);
    }
}
